<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="订单ID" prop="orderId">
            <el-input v-model="queryParams.orderId" placeholder="请输入订单ID" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="费用类别" prop="cateId">
            <el-select v-model="queryParams.cateId" placeholder="请选择费用类别" clearable>
              <el-option v-for="dict in cost_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="报销金额" prop="money">
            <el-input v-model="queryParams.money" placeholder="请输入报销金额" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker v-model="queryParams.createTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择创建时间" />
          </el-form-item>
          <el-form-item label="开票日期" prop="operatorTime">
            <el-date-picker v-model="queryParams.operatorTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择开票日期" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['erp:erpCostOutInfo:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['erp:erpCostOutInfo:edit']" type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['erp:erpCostOutInfo:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['erp:erpCostOutInfo:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="erpCostOutInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="true" label="ID" align="center" prop="id" />
        <el-table-column label="订单ID" align="center" prop="orderId" />
        <el-table-column label="费用类别" align="center" prop="cateId">
          <template #default="scope">
            <dict-tag :options="cost_type" :value="scope.row.cateId" />
          </template>
        </el-table-column>
        <el-table-column label="摘要" align="center" prop="remark" />
        <el-table-column label="报销金额" align="center" prop="money" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="开票日期" align="center" prop="operatorTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.operatorTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="ErpCostOutInfo" lang="ts">
import { listErpCostOutInfo, getErpCostOutInfo, delErpCostOutInfo, addErpCostOutInfo, updateErpCostOutInfo } from '@/api/erp/erpCostOutInfo';
import { ErpCostOutInfoVO, ErpCostOutInfoQuery, ErpCostOutInfoForm } from '@/api/erp/erpCostOutInfo/types';
import { parseTime } from '@/utils/ruoyi';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { cost_type } = toRefs<any>(proxy?.useDict('cost_type'));

const erpCostOutInfoList = ref<ErpCostOutInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const erpCostOutInfoFormRef = ref<ElFormInstance>();
const data = reactive<any>({
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    orderId: undefined,
    cateId: undefined,
    money: undefined,
    createTime: undefined,
    operatorTime: undefined,
    params: {}
  }
});

const { queryParams } = toRefs(data);

/** 查询支出明细列表 */
const getList = async () => {
  loading.value = true;
  const res = await listErpCostOutInfo(queryParams.value);
  erpCostOutInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};



/** 表单重置 */
const reset = () => {
  erpCostOutInfoFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: ErpCostOutInfoVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};


/** 删除按钮操作 */
const handleDelete = async (row?: ErpCostOutInfoVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除支出明细编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delErpCostOutInfo(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'erp/erpCostOutInfo/export',
    {
      ...queryParams.value
    },
    `erpCostOutInfo_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
